package com.whq.thrift.api.kafka.requestlog;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.whq.common.StringUtil;
import com.whq.thrift.api.common.spring.ReloadableConfManager;
import com.whq.thrift.api.kafka.requestlog.db.RequestLog;
import com.whq.thrift.api.kafka.requestlog.service.RequestLogService;

/**
 * 处理具体业务的线程
 * 
 * @author wanghuaiqiang
 * 
 */
public class ParseRequestLogMsgRunnable implements Runnable {
	private static final Logger LOGGER = LoggerFactory.getLogger(ParseRequestLogMsgRunnable.class);
	
	private String requestLogJsonMsg = "";
	private RequestLogService requestLogService = null;
	public ParseRequestLogMsgRunnable(String requestLogJsonMsg, RequestLogService requestLogService, ReloadableConfManager reloadableConfManager) {
		this.requestLogJsonMsg = requestLogJsonMsg;
		this.requestLogService = requestLogService;
	}

	public void run() {
		LOGGER.debug("thread (" + requestLogJsonMsg + ") start");
		RequestLog requestLog = null;
		
		try {
			requestLog = JSON.parseObject(requestLogJsonMsg, RequestLog.class);
		} catch (Exception e) {
			LOGGER.error("parse [" + requestLogJsonMsg + "] to LoginLog exception: ", e);
		}
		
		if (requestLog != null) {
			Map<String, String> params = requestLog.getParams();
			if (params == null) {
				params = new HashMap<String, String>(0);
			}
			
			String netFlag = params.get("netFlag");
			if (StringUtil.isBlank(netFlag)) {
				netFlag = params.get("netflag");
			}
			requestLog.setNetwork(netFlag);
			requestLog.setRequestParams(JSON.toJSONString(params));
			Date date = new Date();
			requestLog.setCreateTime(date);
			
			requestLogService.addRequestLog(requestLog);
		}

		LOGGER.debug("thread (" + requestLogJsonMsg + ") end");
	}
	
	
	
}
